<template>
    <div class="AgentReportDetail">
        <!-- 坐席报备明细表 -->
        <div class="formContainer">
            <!-- 搜索表单模块 -->
            <el-form
                class="searchForm searchForm_inline"
                ref="searchForms"
                :model="searchForm"
                :inline="true"
                label-position="right"
                size="small"
                label-width="110px"
                :rules="rules"
            >
                <el-row>
                    <el-col :lg="7" :xl="6">
                        <el-form-item prop="startTime" label="开始时间:">
                            <el-date-picker
                                v-model="searchForm.startTime"
                                type="datetime"
                                placeholder="请选择开始时间"
                                format="yyyy-MM-dd HH:mm:ss"
                                value-format="yyyy-MM-dd HH:mm:ss"
                                :picker-options="startOptions"
                                @change="startTimeChange"
                            ></el-date-picker>
                        </el-form-item>
                    </el-col>
                    <el-col :lg="7" :xl="6">
                        <el-form-item prop="endTime" label="结束时间:">
                            <el-date-picker
                                v-model="searchForm.endTime"
                                type="datetime"
                                placeholder="请选择结束时间"
                                format="yyyy-MM-dd HH:mm:ss"
                                value-format="yyyy-MM-dd HH:mm:ss"
                                :picker-options="endtOptions"
                                @change="endTimeChange"
                            ></el-date-picker>
                        </el-form-item>
                    </el-col>

                    <el-col :lg="7" :xl="6">
                        <el-form-item prop="teamCodeList" label="工作组组别:">
                            <el-select
                                v-model="searchForm.teamCodeList"
                                multiple
                                placeholder="请选择工作组组别"
                            >
                                <el-option
                                    v-for="item in teamList"
                                    :label="item.teamName"
                                    :key="item.teamCode"
                                    :value="item.teamCode"
                                ></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :lg="7" :xl="6">
                        <el-form-item prop="loginId" label="坐席工号:">
                            <el-input v-model="searchForm.loginId" placeholder="请输入坐席工号"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :lg="7" :xl="6">
                        <el-form-item prop="logoutReason" label="报备类型:">
                            <el-input v-model="searchForm.logoutReason" placeholder="请输入报备类型"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :lg="7" :xl="6" :style="{textAlign: 'right',marginBottom:'5px'}">
                        <el-button
                            type="primary"
                            size="small"
                            icon="el-icon-search"
                            :loading="searchLoading"
                            @click="handleSearch"
                        >查 询</el-button>
                        <el-button
                            type="primary"
                            size="small"
                            icon="el-icon-refresh-right"
                            @click="handleReset"
                        >重 置</el-button>
                        <el-button type="primary" size="small" @click="handleExport">导 出</el-button>
                    </el-col>
                </el-row>
            </el-form>
            <!-- 表格数据展示模块 -->
            <el-table :data="tableData" border stripe class="heard-bg">
                <el-table-column label="坐席报备明细表">
                    <el-table-column
                        :label="`表格时间：${tableHeader.startTime}-${tableHeader.endTime}`"
                    >
                        <el-table-column label="工作组组别">
                            <template slot-scope="scope">{{scope.row.teamName}}</template>
                        </el-table-column>
                        <el-table-column label="坐席工号">
                            <template slot-scope="scope">{{scope.row.loginId}}</template>
                        </el-table-column>
                        <el-table-column label="坐席姓名">
                            <template slot-scope="scope">{{scope.row.loginName}}</template>
                        </el-table-column>
                        <el-table-column label="开始时间">
                            <template slot-scope="scope">{{scope.row.logoutTime}}</template>
                        </el-table-column>
                        <el-table-column label="结束时间">
                            <template slot-scope="scope">{{scope.row.loginTime}}</template>
                        </el-table-column>
                        <el-table-column label="持续时长">
                            <template slot-scope="scope">{{scope.row.loginLongStr}}</template>
                        </el-table-column>
                    </el-table-column>
                    <el-table-column :label="`制表日期：${tableHeader.makeTime}`">
                        <el-table-column label="报备类型">
                            <template slot-scope="scope">{{scope.row.logoutReason}}</template>
                        </el-table-column>
                    </el-table-column>
                </el-table-column>
            </el-table>

            <Pagination
                :hanleChangeCurrentPage="hanleChangeCurrentPage"
                :handleChangeSize="handleChangeSize"
                :total="total"
                :pageSize="pageSize"
                :currentPage="currentPage"
            />
        </div>
    </div>
</template>

<script>
import moment from 'moment';
import Pagination from '@/components/Pagination/Pagination.vue';
import { getDictSelectList, isNull } from '@/utils/Common';
import PageTitle from '@/components/pageTitle/PageTitle.vue';
import request from '@/apis/request';
import { eachProp } from '@/utils/Object';


export default {
    name: 'AgentReportDetail',
    components: {
        Pagination,
        PageTitle,
    },
    data() {
        return {
            currentPage: 1, // 当前分页
            pageSize: 10, // 每页数量
            total: 0, // 数据总数
            searchLoading: false, // 查询按钮loading
            teamList: [], // 工作组下拉列表
            searchForm: { // 搜索表单字段
                startTime: '',//moment(new Date(new Date().getTime() - (1000 * 60 * 60 * 24 * 30))).format('YYYY-MM-DD HH:mm:ss'), // 开始日期 默认为当前日期往前一个月
                endTime: '',//moment(new Date()).format('YYYY-MM-DD HH:mm:ss'), // 截止时间 默认为当前日期
                dateTimeRange: [],
                teamCodeList: [], //工作组多选
                logoutReason: '', //报备类型
            },
            // 表格数据
            tableData: [{}],
            rules: {},
            // 开始日期选择 限制
            startOptions: {
                disabledDate: (time) => {
                    if (this.searchForm.endTime) {
                        return time.getTime() > new Date(moment(this.searchForm.endTime).format('YYYY-MM-DD HH:mm:ss')).getTime();
                    }
                },
            },
            // 结束日期选择限制
            endtOptions: {
                disabledDate: (time) => {
                    if (this.searchForm.startTime) {
                        return time.getTime() < new Date(moment(this.searchForm.startTime).format('YYYY-MM-DD HH:mm:ss')).getTime();
                    }
                },
            },
            tableHeader: { // 表头显示
                startTime: '', //moment(new Date(new Date().getTime() - (1000 * 60 * 60 * 24 * 30))).format('YYYY-MM-DD HH:mm:ss'), // 开始日期 默认为当前日期往前一个月
                endTime: '', //moment(new Date()).format('YYYY年MM月DD日hh时mm分'), // 截止时间 默认为当前日期
                makeTime: '', //moment(new Date()).format('YYYY年MM月DD日'), //制表时间
            },
            tableTotalCount: 0, //查询总计
            spanColumnIndex1: new Map(), //存放第一列的合并情况
            spanProcessRowColumn: {}, //存放合并过程行,列
        };
    },
    created() { },
    mounted() {
        this.searchForm.startTime = moment(new Date(new Date().getTime() - (1000 * 60 * 60 * 24 * 30))).format('YYYY-MM-DD HH:mm:ss');
        this.searchForm.endTime = moment(new Date()).format('YYYY-MM-DD HH:mm:ss');
        this.tableHeader = { // 表头显示
            startTime: moment(new Date(new Date().getTime() - (1000 * 60 * 60 * 24 * 30))).format('YYYY年MM月DD日HH时mm分'), // 开始日期 默认为当前日期往前一个月
            endTime: moment(new Date()).format('YYYY年MM月DD日HH时mm分'), // 截止时间 默认为当前日期
            makeTime: moment(new Date()).format('YYYY年MM月DD日'), //制表时间
        };
        // 初始化方法
        this.init();
    },

    methods: {

        async init() {
            // 获取工作组下拉列表
            this.teamfindAll();

            // 默认查询
            this.getListData();
        },

        startTimeChange() {
            this.tableHeader.startTime = moment(this.searchForm.startTime).format('YYYY年MM月DD日HH时mm分');
        },

        endTimeChange() {
            this.tableHeader.endTime = moment(this.searchForm.endTime).format('YYYY年MM月DD日HH时mm分');
        },

        // 获取工作组下拉列表数据
        teamfindAll() {
            this.$post.busiReport.teamfindAll().then((res) => {
                console.log("teamfindAll-res", res);
                if (res.status == 200 && res.data.success) {
                    this.teamList = res.data.content;
                } else {
                    this.$message.error('查询工作组列表失败', '提示');
                }
            });
        },

        // 获取表格数据
        getListData() {

            const searchParam = {
                ...this.searchForm,
                pageNum: this.currentPage,
                pageSize: this.pageSize,
            };

            // 查询接口
            console.log("getListData-searchParam", searchParam);
            this.$post.busiReport.agentReportDetail(searchParam).then((res) => {
                console.log("getListData-res", res);
                if (res.status == 200 && res.data.success) {
                    this.tableData = res.data.content.recordList;
                    this.total = res.data.content.recordCount;
                    // if (this.tableData.length) {
                    //     this.$message.success('查询成功');
                    // } else {
                    //     this.$alert(res.data.msg || '当前查询条件无数据！', '提示', {
                    //         customClass: 'warning', type: 'warning',
                    //     });
                    // }
                } else {
                    this.$message.error('查询失败', '提示');
                }
            });
        },
        // 点击导出 -> 导出流水记录表格
        handleExport() {
            const data = {
                ...this.searchForm,
            };
            let url = '/iccsrv/busi/report/agentReportDetailDownload?';
            eachProp(data, (prop) => {
                if (!isNull(data[prop])) {
                    url += `${prop}=${data[prop].toString()}&`;
                }
            });
            url = url.substring(0, url.length - 1);
            request({
                url,
                method: 'get',
                responseType: 'blob',
            }).then((res) => {
                // console.log(res, '相应报文')
                const blobUrl = window.URL.createObjectURL(new Blob([res.data]));
                const a = document.createElement('a');
                a.style.display = 'none';
                a.download = `坐席报备明细表-${moment(new Date()).format('YYYYMMDDHHmmss')}.xls`;
                a.href = blobUrl;
                document.body.appendChild(a);
                a.click();
                document.body.removeChild(a);
            });
        },
        // 点击查询
        handleSearch() {
            this.currentPage = 1;
            this.getListData();
        },

        // 点击重置搜索表单
        handleReset() {
            this.$refs.searchForms.resetFields();
            this.currentPage = 1;
            this.getListData();
        },

        // 切换分页大小
        handleChangeSize(val) {
            this.pageSize = val;
            // 重置页码
            this.currentPage = 1;
            this.getListData();
        },

        // 切换页码
        hanleChangeCurrentPage(val) {
            this.currentPage = val;
            this.getListData();
        },

    },
};
</script>
<style lang="scss">
.AgentReportDetail {
    .searchForm {
        .el-select,
        .el-date-editor {
            width: 100%;
        }
    }
}
</style>
<style rel="stylesheet/scss" lang="scss" scoped>
.AgentReportDetail {
    padding: 20px;
    height: 100%;
    background-color: #fff;

    .formContainer .el-input__inner {
        width: 100%;
    }
}
</style>
